package com.student.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.student.bean.Student;
import com.student.dao.Dao;
import com.student.tools.MyUpload;

public class StudentUpdate extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(StudentUpdate.class);
	private Dao dao;

    public StudentUpdate() {
        super();
        dao = new Dao();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/*
	 * 修改并更新用户信息
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		logger.info("User is updating the student's information.");
		
		response.setContentType("text/html");
		PrintWriter pw = response.getWriter();
		
		// 定义DateFormat
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		
		// 从页面获取学生信息
		String no = request.getParameter("no");
		// 禁止用户直接访问 servlet
		if ( no == null ) {
			pw.println("<script>alert('请通过编辑按钮修改学生信息！');window.location.href='StudentList';</script>");			
		}
		String name = java.net.URLDecoder.decode(request.getParameter("name"), "UTF-8");
		String pwd = request.getParameter("pwd");
		String email = request.getParameter("email");
		String entranceTime = request.getParameter("entranceTime");
		String sex = java.net.URLDecoder.decode(request.getParameter("sex"), "UTF-8");
		String photo = request.getParameter("photo");
		String birth = request.getParameter("birth");
		String education = java.net.URLDecoder.decode(request.getParameter("education"), "UTF-8");

		// 从数据库获取学生头像信息
		Student stuInfo = dao.getStudentByNo(Integer.parseInt(no));
		String stuPhotoInfo = stuInfo.getPhoto();
		int index = stuPhotoInfo.lastIndexOf('/');
		String stuPhoto = stuPhotoInfo.substring(index+1);
		
		// 提交学生信息到数据库
		Student stu = new Student();
		stu.setNo(Integer.parseInt(no));
		stu.setName(name);
		stu.setPwd(pwd);
		stu.setEmail(email);
		try {
			stu.setEntranceTime(format.parse(entranceTime));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		stu.setSex(sex);
		
		// 判断是否重新上传头像
		if ( photo.equals(stuPhoto) ) {
			stu.setPhoto("images/student_head_image/"+photo);
		} else {
			stu.setPhoto("images/student_head_image/"+no+"_"+photo);
			// 上传头像
			MyUpload upload = new MyUpload(request, no);
			upload.upload();
		}
		
		try {
			stu.setBirth(format.parse(birth));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		stu.setEducation(education);
		stu.setStatus("在读");
		dao.updateStudent(stu);
		
		// 响应提交
		String result = "提交成功！";
		pw.print(result);
		pw.flush();
		pw.close();
	}

}
